import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:wallet_utils/event_bus_utils.dart';
import 'package:wallet_utils/msg_event_utils.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:wallet_utils/el_refresh.dart';
import 'package:wallet_utils/report_widget/base/callback/base_callback.dart';
import 'package:wallet_utils/toast.dart';
import 'package:wallet_utils/webview_common.dart';
import '../../agreement_dialog.dart';
import '../../login_page/constant.dart';
import '../../product/bean/productBean.dart';
import '../../productPop/product_bootom_sheet.dart';
import '../api/home_api.dart';
import '../bean/popUpsBean.dart';
import '../bean/userAgreementBean.dart';
import 'state.dart';

class HXHAuditRejectLogic extends GetxController {
  final HXHAuditRejectState state = HXHAuditRejectState();
  ProductInfo? productInfo;
  List<ProductList>? productList = [];
  List<PopUpsInfo> popUpsInfo=[];
  @override
  void onInit()  {
    super.onInit();
    EventBusUtils.getInstance().on<MsgEventBusUtils>().listen((event) {
      //是否显示产品状态
      if (event.type == MsgEventBusUtils.productStatus) {
        state.refreshController = RefreshController(initialRefresh: false);
        // obtainProductInfoV2();
        // productProgress();
        getCpaPopList();
      }
    });
  }

  //跳转到第三方链接
  void popover(int type,int index) {
    Future.delayed(Duration.zero, () {
      showDialog<Null>(
        context: Get.context!,
        barrierDismissible: false,
        useSafeArea: false,
        useRootNavigator: false,
        builder: (BuildContext context) {
          return AgreementDialog(
              name: '',
              content: productInfo?.content ?? '',
              submit: '我已阅读并同意授权',
              isClose: true,
              onTap: () {
                Get.back();
                if(type==2){
                  if(productList!.isNotEmpty){
                    addLoanRecord(productList![index].productId!, ShowPage.result);
                  }
                  Get.to(WebViewCommon(
                    productList![index].linkUrl ?? '',
                  ));
                }
              });
        },
      );
    });
  }

  //埋点
  void addLoanRecord(num productId, int showPage) async {
    HomeApi.addLoanRecord(
        productId,
        showPage,
        BaseCallback(
            onSuccess: (map) {},
            onError: (msg) {
              Future.delayed(const Duration(milliseconds: 500), () {
                ToastUtils.showToast(msg ?? "");
              });
            }));
  }

  //弹窗
  void getCpaPopList() async {
    HomeApi.getCpaPopList(
        PopUps().audit,
        BaseCallback(
            onSuccess: (map) {
              PopUpsBean popUpsBean = PopUpsBean.fromJson(map);
              var popUpsInfo = popUpsBean.data!;
              if (popUpsInfo.isNotEmpty) {
                Future.delayed(const Duration(milliseconds: 300), () {
                  ProductBottomSheet.show(
                    Get.context!,
                    logo: productInfo?.imgUrl,
                    name: productInfo?.name,
                    content: productInfo?.content,
                  );
                });
              }
            },
            onError: (msg) {
              Future.delayed(const Duration(milliseconds: 500), () {
                ToastUtils.showToast(msg ?? "");
              });
            }));
  }

}
